﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using System.IO;


namespace Workshop
{
    public partial class FormAddcourse : Form
    {
        public FormAddcourse()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void FormAddcourse_Load(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void btnGoback_Click(object sender, EventArgs e)
        {
            FormCourseinformation courseinformation = new FormCourseinformation();    //create a new FormCourseinformation to jump to this pages
            courseinformation.Show();
            this.Close();       
        }


        //use to create a new attribute when we create a new element
        public XmlAttribute CreateAttribute(XmlNode node, string attributeName, string value)
        {
            try
            {
                XmlDocument doc = node.OwnerDocument;       //createa new xmldocument class which is our own document   
                XmlAttribute attr = doc.CreateAttribute(attributeName);
                attr.Value = value;     //input the value in it
                node.Attributes.SetNamedItem(attr); 
                return attr;
            }
            catch (Exception err)
            {
                string desc = err.Message;
                return null;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtCategory.Text.Trim() != String.Empty && txtStudent.Text.Trim() != String.Empty && txtCourse.Text.Trim() != String.Empty)
            {
                if (txtCategory.Text == "GE Course" || txtCategory.Text == "Major Course")
                {
                    XmlDocument xml = new XmlDocument();
                    xml.Load("Course.xml");         //load the Course.xml
                    String path1 = "//Courses/CourseInfo[@Number]";     //find the attribute of course number
                    XmlNode node1 = xml.SelectSingleNode(path1);
                    int temp;
                    int.TryParse(node1.Attributes[0].Value, out temp);      //change the type from string to integer
                    temp++;         //add 1 course
                    node1.Attributes[0].Value = temp.ToString();        //cover the old value
                    xml.Save("Course.xml");

                    //create a new element named Course in the element of CourseInfo
                    XmlNode courseinfo = xml.SelectSingleNode("/Courses/CourseInfo");
                    XmlElement course = xml.CreateElement("Course");
                    courseinfo.AppendChild(course);

                    //create a new element named CourseID in the element of Course 
                    XmlElement courseID = xml.CreateElement("CourseID");
                    courseID.Attributes.Append(CreateAttribute(courseID, "FirstTime", ""));
                    courseID.Attributes.Append(CreateAttribute(courseID, "SecondTime", ""));
                    courseID.Attributes.Append(CreateAttribute(courseID, "ThirdTime", ""));
                    courseID.InnerText = temp.ToString();
                    course.AppendChild(courseID);

                    //create a new element named Subject in the element of Course
                    XmlElement subject = xml.CreateElement("Subject");
                    subject.InnerText = txtCourse.Text;
                    course.AppendChild(subject);

                    //create a new element named Category in the element of Course
                    XmlElement category = xml.CreateElement("Category");
                    category.InnerText = txtCategory.Text;
                    course.AppendChild(category);

                    //create a new element named Student in the element of Course
                    XmlElement student = xml.CreateElement("Student");
                    student.InnerText = txtStudent.Text;
                    course.AppendChild(student);

                    //create a new element named TAID in the element of Course
                    XmlElement taID1 = xml.CreateElement("TAID1");
                    taID1.Attributes.Append(CreateAttribute(taID1, "TAName", ""));
                    taID1.InnerText = null;
                    course.AppendChild(taID1);

                    //create a new element named TAID in the element of Course
                    XmlElement taID2 = xml.CreateElement("TAID2");
                    taID2.Attributes.Append(CreateAttribute(taID2, "TAName", ""));
                    taID2.InnerText = null;
                    course.AppendChild(taID2);


                    //create a new element named Instructor in the element of Course
                    XmlElement instructor = xml.CreateElement("InstructorID");
                    instructor.Attributes.Append(CreateAttribute(instructor, "InstructorName", ""));
                    instructor.InnerText = null;
                    course.AppendChild(instructor);

                    //create a new element named it label
                    XmlElement label = xml.CreateElement("label");
                    label.InnerText = null;
                    course.AppendChild(label);

                    //save the xml file
                    xml.Save("Course.xml");

                    //go back to the Courseinformation page
                    FormCourseinformation courseinformation = new FormCourseinformation();
                    courseinformation.Show();
                    this.Close();       //close this page
                }
                else
                {
                    MessageBox.Show("Please input the Category: GE Course (GE), Major Course (Major Course) ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("The Textbox is empty!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}
